#import "ForgetPasswordViewController.h"
#import "PhoneNumberChecker.h"
#import "RequestServerAPI.h"

@interface ForgetPasswordViewController ()

@end

@implementation ForgetPasswordViewController
@synthesize authCodeButton;

- (void)viewDidLoad {
    [super viewDidLoad];
    self.phoneNumberInput.delegate = self;
    // Do any additional setup after loading the view from its nib.
    authCodeButton.layer.cornerRadius = 5;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"忘记密码";
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
    self.navigationItem.backBarButtonItem=backButton;
    
    _phoneNumberInput.layer.cornerRadius=4.0f;
    _phoneNumberInput.layer.masksToBounds=YES;
    _phoneNumberInput.layer.borderColor=[[UIColor redColor]CGColor];
    _phoneNumberInput.layer.borderWidth= 1.0f;
    
    UILabel *leftView = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60, 40)];
    leftView.text =@"+86";
    leftView.textColor = [UIColor whiteColor];
    leftView.textAlignment = NSTextAlignmentCenter;
    leftView.backgroundColor = UIColorMake(255, 111, 111);
    _phoneNumberInput.leftView = leftView;
    _phoneNumberInput.leftViewMode = UITextFieldViewModeAlways;
}

- (void)viewWillDisappear:(BOOL)animated {

    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (IBAction)authCodeButton:(id)sender {
    if (touchEnable == true) {
        NSLog(@"getAuthCode.");
        [RequestServerAPI apiRegisterPhone:self phoneNumber:self.phoneNumberInput.text type:@"1" codetype:@"2" superFlag:2];
    }
}

- (IBAction)backButton:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"running?");
    if (range.location > 10) {
        return NO;
    }else{
        if (textField.text.length == 10) {
            NSString *s = [textField.text stringByAppendingString:string];
            if ([PhoneNumberChecker isValidNumber:s]) { //检查号码是否合法
                self.authCodeButton.backgroundColor = DefaultNavigationColor;
                touchEnable = true;
            }
        }else{
            self.authCodeButton.backgroundColor = [[UIColor alloc]initWithRed:0.8 green:0.8 blue:0.8 alpha:1];
            touchEnable = false;
        }
        return YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
